package com.cmbchina.logistics.shipping.antiCorruption.packaging;

import com.cmbchina.logistics.shipping.entity.Container;
import com.cmbchina.logistics.shipping.entity.Package;
import com.cmbchina.logistics.shipping.entity.Product;
import com.cmbchina.warehouse.packaging.antiCorruption.shipping.IPackagingServiceFacade;
import com.cmbchina.warehouse.packaging.entity.Item;
import com.cmbchina.warehouse.packaging.entity.Warehouse;

import java.util.List;

/**
 * 适配器模式
 */
public class PackagingServiceAdapter implements  IPackagingService{
    private IPackagingServiceFacade packagingServiceFacade;
    //模型转换器
    private IMapper mapper;

    public PackagingServiceAdapter(IPackagingServiceFacade packagingServiceFacade, IMapper mapper) {
        this.packagingServiceFacade = packagingServiceFacade;
        this.mapper = mapper;
    }

    @Override
    public List<Package> packageItems(List<Product> products, List<Container> containers) {
        //逻辑初始化一个瞬态虚拟仓库
        Warehouse warehouse = new Warehouse();
        //使用转换器将Product模型映射到Item
        List<Item> items = mapper.map(products);
        //门面模式调用
        List<com.cmbchina.warehouse.packaging.entity.Package> packages = packagingServiceFacade.packageItems(items, warehouse);
        //将Package的结果映射到Shipping中定义的Package模型
        List<Package> currentPackages = mapper.map(packages);
        return currentPackages;
    }
}
